<?php
class Admin_CategoriesController extends Zend_Controller_Action
{
    protected $_flashMessenger = null;

    public function init()
    {
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }

    function indexAction()
    {
        $categories = Doctrine::getTable('Category')->getTree()->fetchTree();
        $this->view->categories = $categories;

        $this->view->flash = $this->_flashMessenger->getMessages();
    }

    function addAction()
    {
        $form = new CategoryForm();
        $form->submit->setLabel('Add');

        $pairs = Doctrine::getTable('Category')->fetchPairs('id', 'name');
        $parentSelect = $form->getElement('parent_id');
        $parentSelect->addMultioption('0', '---Select category---');
        $parentSelect->addMultioptions($pairs);

        $this->view->form = $form;

        if($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if($form->isValid($formData))
            {
            	$filtered_data = $form->getValues(true);

                $category = new Category();
                $category->merge($filtered_data);

                $parent = Doctrine::getTable('Category')
                    ->find($formData['parent_id']);

                $category->getNode()->insertAsLastChildOf($parent);

                $this->_flashMessenger->addMessage('Category created!');
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }
    }

    function editAction()
    {
        $form = new CategoryForm();
        $form->submit->setLabel('Save');

        $pairs = Doctrine::getTable('Category')->fetchPairs('id', 'name');
        $parentSelect = $form->getElement('parent_id');
        $parentSelect->addMultioption('0', '---Select category---');
        $parentSelect->addMultioptions($pairs);
        
        $category = $this->_findRecord();

        $this->view->form = $form;

        if($this->_request->isPost())
        {
            $formData = $this->_request->getPost();

            if($form->isValid($formData))
            {
                $filtered_data = $form->getValues(true);

                $category->merge($filtered_data);
                $category->save();

                $parent = Doctrine::getTable('Category')
                    ->find($formData['parent_id']);

                if(!$category->getNode()->isRoot())
                    if($caterory->id != $id)    //  wybrana kategoria-przodek != bieżąca kategoria
                        $category->getNode()->moveAsLastChildOf($parent);

                $this->_flashMessenger->addMessage('Category updated!');
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }
        else
        {
            $form->populate($category->toArray());

            if(!$category->getNode()->isRoot())
                $parentSelect->setValue($category->getNode()->getParent()->id);
        }
    }

    function deleteAction()
    {
        $category = $this->_findRecord();

        if($this->_request->isPost())
        {
            $del = $this->_request->getPost('del');
            if ($del == 'Yes')
            {
                $category->getNode()->delete();

                $this->_flashMessenger->addMessage('Category deleted!');
            }
            $this->_helper->redirector('index');
        }
        else
        {
            $this->view->category = $category;
        }
    }
    
    /**
     * Finds record in database
     * 
     * @return Doctrine_Record
     */
    protected function _findRecord()
    {
        $id = (int) $this->_request->getParam('id');
        $record = Doctrine::getTable('Category')->find($id);
        if (!$record) throw new Zend_Controller_Dispatcher_Exception();

        return $record;
    }     
    
}
?>